﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Wmi
{
    public class WmiQueryClass
    {
        public WmiQueryClass()
        {
            MachineName = Environment.MachineName;
        }

        public string MachineName
        {
            get
            {
                if (scope == null)
                    return Environment.MachineName;
                else
                    return scope.Path.Server;
            }
            set
            {
                scope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", value));
            }
        }

        private ManagementScope scope;

        public void Get(string classPath, Predicate<ManagementObjectCollection> operation)
        {
            if (!scope.IsConnected)
                scope.Connect();

            ObjectQuery query = new ObjectQuery("SELECT * FROM " + classPath);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            ManagementObjectCollection collection = searcher.Get();
            operation(collection);
        }

        public void MonitorEvent(string classpath)
        {
            if (!scope.IsConnected)
                scope.Connect();

            ManagementEventWatcher watch = new ManagementEventWatcher(scope, new WqlEventQuery("SELECT * FROM " + classpath));
            watch.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            watch.Start();
            Console.WriteLine();
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
            watch.Stop();

        }

        void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            foreach (var @event in e.NewEvent.Properties)
            {
                Console.WriteLine("{0}, {1}", @event.Name, @event.Value);
            }
        }

        #region IDisposable Members


        #endregion


        public static void Main()
        {

            WmiQueryClass wq = new WmiQueryClass();

            Predicate<ManagementObjectCollection> act = (collection) =>
                {
                    foreach (ManagementObject m in collection)
                    {
                        Console.WriteLine("Computer Name : {0}",
                            m["csname"]);
                        Console.WriteLine("Windows Directory : {0}",
                            m["WindowsDirectory"]);
                        Console.WriteLine("Operating System: {0}",
                            m["Caption"]);
                        Console.WriteLine("Version: {0}", m["Version"]);
                        Console.WriteLine("Manufacturer : {0}",
                            m["Manufacturer"]);
                    }
                    return false;
                };
            wq.Get("Win32_OperatingSystem", act);

            wq.MonitorEvent("Win32_ProcessStartTrace");
        }
    }

}
